package com.nulldev.util.web.YouTubeExtractor.util;

import java.util.Calendar;
import java.util.Date;

import com.nulldev.util.data.Variables;
import com.nulldev.util.web.YouTubeExtractor.extractors.util.YTIAPIClientType;

public class YTEClientDateGenerator {

	private static Calendar toCalendar(final Date date) {
		final Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal;
	}

	public static String makeDate(final Date date, final YTIAPIClientType clientType) {
		Variables.requireNonNull(date, "date");
		Variables.requireNonNull(clientType, "clientType");
		return makeDate(toCalendar(date), clientType);
	}

	public static String makeDate(final Calendar cal, final YTIAPIClientType clientType) {
		Variables.requireNonNull(cal, "cal");
		Variables.requireNonNull(clientType, "clientType");
		final int year = cal.get(Calendar.YEAR);
		final int month = cal.get(Calendar.MONTH);
		final int day = cal.get(Calendar.DAY_OF_MONTH);
		switch (clientType) {
			case WEB:
			case MOBILE_WEB:
				return String.format("2.%i%i%i.00.00", year, month, day);
			case EMBEDDED_WEB:
			case WEB_YT_CREATOR:
			case WEB_STUDIO:
				return String.format("1.%i%i%i.00.00", year, month, day);
			case WEB_YT_MUSIC:
				return String.format("1.%i%i%i.01.00", year, month, day);
			case WEB_UNPLUGGED:
			case WEB_PARENT_TOOLS:
				return String.format("1.%i%i%i", year, month, day);
			case TVHTML5:
			case TVHTML5_FOR_KIDS:
				return String.format("7.%i%i%i", year, month, day);
			case MOBILE_WEB_TIER2:
				return String.format("9.%i%i%i", year, month, day);
			default:
				throw new UnsupportedOperationException("Unknown generation format for client: " + clientType);
		}
	}
}
